////////////////////////////////////////////////////////////////////////////////
//
//  This program is free software; you can redistribute it and/or modify 
//  it under the terms of the GNU General Public License as published by the 
//  Free Software Foundation; either version 3 of the License, or (at your 
//  option) any later version.
//
//  This program is distributed in the hope that it will be useful, but 
//  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
//  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
//  for more details.
//
//  You should have received a copy of the GNU General Public License along 
//  with this program; if not, see <http://www.gnu.org/licenses>.
//
////////////////////////////////////////////////////////////////////////////////

package merapi.gps.usglobalstat.bu353.driver;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.InputStream;
import java.io.OutputStream;


import merapi.gps.driver.IGPSDriver;

/**
 *  An implementation of IGPSDriver for the USGlobalStat BU-353 hardware.
 *  
 *  @author Adam Flater - adamflater.net
 */
public class USGlobalStat_BU_353_Driver implements IGPSDriver 
{
    //--------------------------------------------------------------------------
    //
    //  Class constants
    //
    //--------------------------------------------------------------------------

    /**
     *  The port name for MAC OS X.
     */
	public static final String PORT_NAME_MAC = "/dev/tty.usbserial";

	/**
	 *  The port name for Windows
	 */
	public static final String PORT_NAME_WIN = "COM3";
	
	/**
	 *  The serial baud rate.
	 */
	public static final int BAUD_RATE = 4800;
	

	//--------------------------------------------------------------------------
	//
	//  Class properties
	//
	//--------------------------------------------------------------------------
	
	/**
	 *  The port name used to connect to the gps hardware.
	 */
	public static String PORT_NAME = null;
	
	/**
	 *   This static block is used to initialize PORT_NAME based on the OS
	 */
	static
	{
		if ( System.getProperty( "os.name" ).toUpperCase().contains( "MAC" ) )
		{
			PORT_NAME = PORT_NAME_MAC;
		}
		else
		{
			PORT_NAME = PORT_NAME_WIN;
		}
	}

	
	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------

	/** 
	 *  Read-only inputStream property.
	 */
	public InputStream getInputStream() 
	{
		if ( __serialPort != null )
		{
			try
			{
				return __serialPort.getInputStream();
			}
			catch ( Exception e )
			{
				return null;
			}
		}
		else
		{
			return null;
		}
	}

	/** 
	 *  Read-only outputStream property.
	 */
	public OutputStream getOutputStream() 
	{
		if ( __serialPort != null )
		{
			try
			{
				return __serialPort.getOutputStream();
			}
			catch ( Exception e )
			{
				return null;
			}
		}
		else
		{
			return null;
		}
	}	
	
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------

	/**
	 *  Opens a connection to the USGlobalStat BU-353
	 */
	public boolean connect() 
	{
		try 
		{
			CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier( PORT_NAME );
			if ( portIdentifier.isCurrentlyOwned() )
			{
				return false;
			}
			else 
			{
				__serialPort = (SerialPort)portIdentifier.open( "USGlobalStat_BU_353_Driver", 200 );
				__serialPort.setSerialPortParams( BAUD_RATE, SerialPort.DATABITS_8, 
			              			              SerialPort.STOPBITS_1, SerialPort.PARITY_NONE );
			}
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}
		
		return true;
	}

	/**
	 *  Disconnects the USGlobalStat BU-353
	 */
	public boolean disconnect() 
	{
		__serialPort.close();
		return true;
	}


	//--------------------------------------------------------------------------
	//
	//  Variables
	//
	//--------------------------------------------------------------------------

	/**
	 *  The SerialPort connected to the gps hardware.
	 */
	protected SerialPort __serialPort = null; 
}
